package de.jmda.mview.proj;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.border.TitledBorder;

import org.apache.log4j.Logger;

import de.jmda.gui.swing.mvc.ControllerContextProvider;
import de.jmda.gui.swing.mvc.ControllerSupportDisabled;
import de.jmda.gui.swing.mvc.ControllerSupportEnabled;
import de.jmda.mproc.TypeElementDictionarySingleton;
import de.jmda.mproc.swingui.JPanelVisualProcessor;

@ControllerSupportEnabled
public class Diagrams extends JPanel implements ControllerContextProvider
{
	@ControllerSupportDisabled
	private static final long serialVersionUID = 1L;
	
	@ControllerSupportDisabled
	private final static Logger LOGGER = Logger.getLogger(Diagrams.class);

	@ControllerSupportDisabled
	private DiagramsModel model;

	/**
	 * The visual processor associated to this component. If present {@link
	 * JPanel2VisualProcessor#launchProcessor()} will be used to try to populate
	 * "empty {@link TypeElementDictionarySingleton} instance.
	 */
	private JPanelVisualProcessor visualProcessor;
	
	// the sub components of this component 
	private JSplitPane splitPaneRoot;
	private JSplitPane splitPaneLeft;
	private JScrollPane scrollPaneTypes;
	private JScrollPane scrollPaneDiagrams;
	private JTree treeTypes;
	private JTree treeDiagrams;
	private JTabbedPane tabbedPaneDiagrams;

	/**
	 * Create the panel.
	 */
	public Diagrams()
	{
		this(DiagramsModel.createDefaultDiagramsModel(), null);
	}

	public Diagrams(DiagramsModel model, JPanelVisualProcessor visualProcessor)
  {
		if (model == null)
		{
			model = DiagramsModel.createDefaultDiagramsModel();
		}
		
		if (visualProcessor == null)
		{
			LOGGER.debug("null visual processor");
		}

		// store the model
		this.model = model;
		// store the visual processor
		this.visualProcessor = visualProcessor;

		// initialise view (component container)
		initialize();

		// create view components
		// - no view components to create for now

		// new view components have been created that the controller does not yet
		// know, so make controller update itself with a fresh context
		// - no view components created for now
		controller.refreshControllerContext();
		
		// set controller model (updates view internally)
		controller.setModel(model);
  }

	private void initialize()
	{
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		splitPaneRoot = new JSplitPane();
		GridBagConstraints gbc_splitPaneRoot = new GridBagConstraints();
		gbc_splitPaneRoot.fill = GridBagConstraints.BOTH;
		gbc_splitPaneRoot.gridx = 0;
		gbc_splitPaneRoot.gridy = 0;
		add(splitPaneRoot, gbc_splitPaneRoot);
		
		splitPaneLeft = new JSplitPane();
		splitPaneLeft.setResizeWeight(0.5);
		splitPaneLeft.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPaneRoot.setLeftComponent(splitPaneLeft);
		
		scrollPaneTypes = new JScrollPane();
		scrollPaneTypes.setViewportBorder(new TitledBorder(null, "types", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		splitPaneLeft.setLeftComponent(scrollPaneTypes);
		
		treeTypes = new JTree();
		scrollPaneTypes.setViewportView(treeTypes);
		
		scrollPaneDiagrams = new JScrollPane();
		scrollPaneDiagrams.setViewportBorder(new TitledBorder(null, "diagram groups", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		splitPaneLeft.setRightComponent(scrollPaneDiagrams);
		
		treeDiagrams = new JTree();
		treeDiagrams.setRootVisible(false);
		scrollPaneDiagrams.setViewportView(treeDiagrams);
		
		tabbedPaneDiagrams = new JTabbedPane(JTabbedPane.TOP);
		tabbedPaneDiagrams.setBorder(new TitledBorder(null, "diagrams", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		splitPaneRoot.setRightComponent(tabbedPaneDiagrams);
	}
	//GEN-BEGIN:controller support                                                                                   
	@de.jmda.gui.swing.mvc.ControllerSupportDisabled                                                 
	private de.jmda.mview.proj.DiagramsController controller =                                                   
			new de.jmda.mview.proj.DiagramsController(this);                                                         
                                                                                                    
	/**                                                                                                
	 * @see de.jmda.gui.swing.mvc.ControllerContextProvider#getControllerContext()                   
	 */                                                                                                
	@Override // make sure that this class implements de.jmda.gui.swing.mvc.ControllerContextProvider
	public de.jmda.gui.swing.mvc.ControllerContext getControllerContext()                            
	{                                                                                                  
		return new de.jmda.mview.proj.DiagramsControllerContext(
				this,
				visualProcessor,
				splitPaneRoot,
				splitPaneLeft,
				scrollPaneTypes,
				scrollPaneDiagrams,
				treeTypes,
				treeDiagrams,
				tabbedPaneDiagrams);                                                         
	}                                                                                                  
	//GEN-END:controller support
}